package net.oschina.aibinxiao.wechatauth.servlet;

import net.oschina.aibinxiao.wechatauth.util.AuthUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

@WebServlet("/wxLogin")
public class LoginServlet extends HttpServlet{

    // 使用ngrok内网映射工具，将本机服务器映射到公网地址：http://feiyucoding.tunnel.echomod.cn/wechat-auth/index.html
    // 即可通过公网访问，本地服务，只要保证本地服务开启

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String backUrl = "http://feiyucoding.tunnel.echomod.cn/wechat-auth/callBack";
        String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + AuthUtil.APPID
                + "&redirect_uri=" + URLEncoder.encode(backUrl)
                + "&response_type=code"
                + "&scope=snsapi_userinfo"
                + "&state=STATE#wechat_redirect";
        resp.sendRedirect(url);
    }
}
